package com.walala.logic.dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

/**
 * @author KayLi
 */
public class DataSourceManager {

    private DataSourceManager(){}

    private static DataSourceManager instance = new DataSourceManager();

    public static DataSourceManager getInstance(){
        return instance;
    }

    private SqlSession sqlSession;

    private MsgDao msgDao;
    private SessionDao sessionDao;

    public void init() throws Exception{
        Reader reader = Resources.getResourceAsReader("config/mybatis.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        msgDao = sqlSession.getMapper(MsgDao.class);
        sessionDao = sqlSession.getMapper(SessionDao.class);
    }

    public MsgDao getMsgDao(){
        return msgDao;
    }

    public SessionDao getSessionDao(){
        return sessionDao;
    }

}
